from typing import TypeAlias

# 设置项的值的类型暂定为这几个。备忘：导出 float 一定至少保留一位小数与整数区分
ValueType: TypeAlias = str|bool|int|float|None

class SettingItem:
    """单个设置条目"""
    def __init__(self, item_id: str = "", value: ValueType = None):
        # 唯一区分标识
        self.id: str = item_id
        # 值
        self.value: ValueType = value
        # 显示的名称（由语言包加载）
        self.name: str = ""
        # 显示的描述（由语言包加载）
        self.description: str = ""


class SettingCategory:
    """单个设置类别"""
    def __init__(self, category_id: str = ""):
        self.id: str = category_id
        self.items: dict[str, SettingItem] = {}
        self.name: str = ""
        self.description: str = ""

    def add_item(self, item: SettingItem):
        self.items[item.id] = item

    def get_item_value(self, item_id: str) -> ValueType:
        return self.items[item_id].value


class SettingManager:
    """设置管理器，掌管设置的读取和保存"""
    def __init__(self):
        self.categories: dict[str, SettingCategory] = {}

    def add_category(self, category: SettingCategory):
        """在创建管理器结构时使用，以后不用（需要设置访问限制？）"""
        self.categories[category.id] = category

    def get_item_value(self, category_id: str, item_id: str) -> ValueType:
        """获取某一个条目，目的为读。未设置访问控制，请不要获取后修改设置"""
        return self.categories[category_id].get_item_value(item_id)

    def read_from_disk(self):
        """从文件读取"""
        a = {
            "name": "Settings",
            "categories": [
                {
                    "id": "screen",
                    "items": [
                        {
                            "id": "size_x",
                            "value": 23
                        }, {
                            "id": "size_y",
                            "value": 24
                        }
                    ]
                }, {
                    "id": "keyboard",
                    "items": [
                        {
                            "id": "time",
                            "value": 0.2
                        }, {
                            "id": "number",
                            "value": 26
                        }
                    ]
                }
            ]
        }
        for cats in a["categories"]:
            cat = SettingCategory(cats["id"])
            for items in cats["items"]:
                item = SettingItem(items["id"], items["value"])
                cat.add_item(item)
                print(items["id"], items["value"])
            self.add_category(cat)



    def save_to_disk(self):
        """保存到文件"""
        pass

c = SettingManager()
c.read_from_disk()
